//
//  KBNewFeatureViewController.swift
//  KimboWeiBoSwift
//
//  Created by kimbo on 15/12/19.
//  Copyright © 2015年 kimbo. All rights reserved.
//

import UIKit

private let reuseIdentifier = "collectionViewCell"

class KBNewFeatureViewController: UICollectionViewController {

    // MARK: - 属性
    /// 流水布局
    private let layout = UICollectionViewFlowLayout()
    
    /// cell的个数
    private let itemNum = 4
    
    // MARK: - 方法
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    init () {
        super.init(collectionViewLayout: layout)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        setupItem()
    }

    //设置item相关属性
    func setupItem() {
        //注册cell
        self.collectionView!.registerClass(KBNewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
    }
    
    // MARK: - UICollectionView数据源方法
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return itemNum
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! KBNewFeatureCell
    
        cell.index = indexPath.item // 这里用indexPath.item
    
        return cell
    }
    
    //停止划动时,显示按钮动画
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        
        //这里的indexPath是已经消失的indexPath,而我们需要正在显示的indexPath
        
        //正在显示的indexPath(最后一个)
        let visibleIndexPath = collectionView.indexPathsForVisibleItems().last!

        if visibleIndexPath.item == itemNum - 1 { //最后一页
            
            //获取visibleIndexPath对应的cell
            let cell = collectionView.cellForItemAtIndexPath(visibleIndexPath) as! KBNewFeatureCell
            
            cell.startButtonAnimation()
        }
    }


}




// MARK: - 自定义cell
class KBNewFeatureCell: UICollectionViewCell {
    
    // MARK: - 属性
    ///图片索引,利用属性监视器改变图片
    var index = 0 {
        didSet {
            bgImageView.image = UIImage(named: "new_feature_\(index + 1)")
            
            startButton.hidden = true
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    //设置UI
    private func setupUI() {
    
        contentView.addSubview(bgImageView)
        contentView.addSubview(startButton)
        
        //添加约束
//        contentView.addConstraint(NSLayoutConstraint(item: bgImageView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        /* 背景图片 */
        bgImageView.ff_Fill(contentView)
        
        
        /* 开始按钮 */
        startButton.ff_AlignInner(type: ff_AlignType.BottomCenter, referView: contentView, size: nil, offset: CGPoint(x: 0, y: -160))
    }
    
    //开始按钮动画
    func startButtonAnimation() {
        
        startButton.hidden = false
        
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        
        UIView.animateWithDuration(1.0, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions.CurveEaseOut, animations: { () -> Void in
            
            self.startButton.transform = CGAffineTransformIdentity
            
            }) { (_) -> Void in
                print("动画结束")
        }
    }
    
    //点击开始按钮，切换到首页
    @objc private func startButtonDidClick() {
        AppDelegate.switchRootViewController(true)
    }
    
    // MARK: - 懒加载
    /* 背景图片 */
    private lazy var bgImageView = UIImageView()
    
    /* 开始按钮 */
    private lazy var startButton: UIButton = {
        
        let button = UIButton()
        
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        
        button.setTitle("开始体验", forState: UIControlState.Normal)
        
        button.addTarget(self, action: "startButtonDidClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
}


